﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 自引用的组织结构树
{
    public class OrgUnitConfig : IEntityTypeConfiguration<OrgUnit>
    {
        public void Configure(EntityTypeBuilder<OrgUnit> builder)
        {
            builder.ToTable("T_OrgUnits");
            builder.Property(x => x.Name).IsUnicode().HasMaxLength(50).IsRequired();

            //根节点没有Parent，因此不能修饰为“不可为空”
            builder.HasOne<OrgUnit>(x => x.Parent).WithMany(x => x.Children).OnDelete(DeleteBehavior.NoAction);
        }
    }
}
